<?php

namespace app\cps\controller;

use think\facade\Request;
use app\common\Common;
use app\cps\service\OfficialAccountService;

class Keyword extends Base
{
    private $service;

    public function __construct(OfficialAccountService $service)
    {
        parent::__construct();
        $this->service = $service;
    }

    // 渲染关键字列表页面
    public function index()
    {
        return view();
    }

    // 关键字列表
    public function list()
    {
        $params = Request::param();

        $data = $this->service->keywordReplyList($params, $this->getUserInfo());

        return Common::success($data);
    }

    // 渲染关键字详情页面
    public function info()
    {
        return view();
    }

    // 关键字回复详情
    public function detail()
    {
        $params = Request::param();

        try {
            validate(\app\cps\validate\OfficialAccount::class)->scene('detail')->check($params);
        } catch (\Exception $e) {
            return Common::fail($e->getError());
        }

        $data = $this->service->getKeywordReplyDetail($params['id']);

        return Common::success($data);
    }

    // 添加关键字回复
    public function add()
    {
        $params = Request::param();

        try {
            validate(\app\cps\validate\OfficialAccount::class)->scene('add_keyword')->check($params);
        } catch (\Exception $e) {
            return Common::fail($e->getError());
        }

        $result = $this->service->editKeywordReply($params, $this->getUserInfo());

        if (!$result) {
            return Common::fail();
        }

        return Common::success();
    }

    // 修改关键字回复
    public function edit()
    {
        $params = Request::param();

        try {
            validate(\app\cps\validate\OfficialAccount::class)->scene('edit_keyword')->check($params);
        } catch (\Exception $e) {
            return Common::fail($e->getError());
        }

        $result = $this->service->editKeywordReply($params, $this->getUserInfo());

        if (!$result) {
            return Common::fail();
        }

        return Common::success();
    }

    // 渲染关键字订单统计页面
    public function order()
    {
        return view();
    }

    // 获取指定关键字产生的订单列表
    public function orderList()
    {
        $params = Request::param();

        try {
            validate(\app\cps\validate\OfficialAccount::class)->scene('order_list')->check($params);
        } catch (\Exception $e) {
            return Common::fail($e->getError());
        }

        $data = $this->service->getKeywordReplyOrderList($params);

        return Common::success($data);
    }
}